package com.huanwei.mini.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.huanwei.mini.R;
import com.huanwei.mini.adapter.DemoAdapter;
import com.huanwei.mini.http.API;
import com.huanwei.mini.http.TZ;
import com.huanwei.mini.utils.JSONUtils;
import com.huanwei.mini.utils.Okhttp;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;



/**
 *分类
 */
public class TZ_Fragment extends Fragment {
    View view;

    Unbinder unbinder;
    @BindView(R.id.lv)
    ListView lv;
    int  type=0;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = View.inflate(getActivity(), R.layout.tz, null);
            ButterKnife.bind(this, view);
            setdata();

        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }

        unbinder = ButterKnife.bind(this, view);
        return view;
    }
    DemoAdapter  demoAdapter;
    private void setdata() {
           demoAdapter=new DemoAdapter();
        lv.setAdapter(demoAdapter);
        if (type==1) {
            return;

        }
        API.CC(getActivity(), new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                type=1;
                Log.e("xxx", "1111"+st);
                TZ tz = JSONUtils.parseJSON(st, TZ.class);
                if (tz.getError().equals("0")) {
                    List<TZ.DataEntity> data = tz.getData();
                    demoAdapter.addItem(data);
                }else {
                    Toast.makeText(getActivity(), ""+tz.getMsg(), Toast.LENGTH_SHORT).show();
                }


            }





            @Override
            public void onFalia(int code, String errst) {
                type=0;
                Toast.makeText(getActivity(), "网络错误"+errst, Toast.LENGTH_SHORT).show();
            }
        });

    }



}
